$(function () {
    var historyDatagrid = $("#history_datagrid");
    var query_dialog = $("#query_dialog");
    var queryForm = $("#query_form");
    historyDatagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/transferHistory/list",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: "#history_toolbar",
        columns: [[
            {field: 'client.name', title: '潜在客户姓名', formatter:formatterClientName,width: 100, align: 'center'},
            {field: 'client.qq', title: 'QQ', formatter:formatterQQ,width: 100, align: 'center'},
            {field: 'client.tel', title: '联系电话', formatter:formatterTel,width: 100, align: 'center'},
            {field: 'transferDate', title: '日期', width: 100, align: 'center'},
            {field: 'oldSaleman', title: '原拥有者', width: 100, align: 'center'},
            {field: 'newSaleman', title: '移交目标', width: 100, align: 'center'}
        ]]
    });


    //x渲染高级查询对话框
    query_dialog.dialog({
        width: 300,
        height: 255,
        title: '移交历史高级查询',
        buttons: "#clientQuery_btns",
        closed: true
    })


    var cmdObj = {
        //刷新
        'reload':function () {
            historyDatagrid.datagrid("reload");
        },
        //打开高级查询对话框
        'query':function () {
            //清除表单数据
            queryForm.form('clear')
            //打开对话框
            query_dialog.dialog('open');
        },
        //关闭高级查询对话框
        'cancelQuery':function () {
            query_dialog.dialog('close');
        },
        //高级查询
        'submitForm':function () {
            queryForm.form('submit', {
                url:'/transferHistory/list',
                success:function(pageResult){
                    //数据列表加载数据
                    var pageResult = $.parseJSON(pageResult);
                    historyDatagrid.datagrid("loadData",pageResult);
                    //关闭查询对话框
                    cmdObj.cancelQuery();
                }
            });
        }

    }

    //统一给data-cmd绑定点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    });
})


function formatterClientName(value,row,index) {
    return row.client.name;
}

function formatterQQ(value,row,index) {
    if (row.client.qq) {
        return row.client.qq;
    }
}

function formatterTel(value,row,index) {
    if (row.client.tel) {
        return row.client.tel;
    }
}